home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / misc / sci / RARS_Amiga_3.lha / RARS / cntrl0.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-27  |  8.5 KB  |  170 lines

  1. // CNTRL0.CPP - "driver" function for RARS - M. Timin, Feb. 1995
  2. // adapted to ver. 0.39 3/6/95 by M. Timin
  3. // for ver. 0.60
  4.  
  5.              /* Easy-To-Understand Robot Driver */
  6. /*
  7. This robot driver attempts to stay in the middle of the track at all
  8. times.  She calculates a cornering speed for each corner based on its
  9. radius.  She accelerates on each straightaway until a certain fraction
  10. of its length is reached; Then she slows down, attempting to arrive at
  11. the corner with the proper cornering speed.  In the corner she attempts
  12. to maintain the cornering speed while attempting to stay in the middle
  13. of the track.  Her strategy for passing is to choose right or left at
  14. random, and then throw the car into a sharp slide toward that direction.
  15. (pretty ballsy, huh!)
  16. */
  17. // The language here can be considered to be ANSI C or C++.
  18.  
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include "car.h"
  23.  
  24. // These parameters may be adjusted to get better performance:
  25. const double CORN_SPD_CON = 5.7;   // determines how fast to take corners
  26. const double STEER_GAIN = 2.5;     // servo gain, for staying in "lane"
  27. const double STEER_DAMP = 1.5;     // servo damping, to prevent "weaving"
  28. const double END_ACCEL = .35; // we accelerate until this fraction of length
  29. const double END_CORNER = 2.3;// used to decide when to start leaving the corer
  30. const double ESC_ALPHA = .03; // after a crash, set alpha like this, + or -
  31. const double SLIP_LIM = 7.0;  // maximum wheel slip, ft/sec, in wheel_slip()
  32. const double SLIP_CON = 200.0; //
  33. const double SHARP_TURN = .05; // change in alpha when attempting to pass
  34. const int PASSING_TIME = 50;  // time to stay in passing maneuver, counts
  35.  
  36. // The following function calculates the speed for a corner.
  37. // The lateral force produced by cornering is proportional to the square
  38. // of the speed, and is inversely proportional to the radius of the path.
  39. // Therefore, the attainable cornering speed for differnt radii is
  40. // proportional to the square root of the radius.  This function implements
  41. // that rule.  The value to use for CORN_SPD_CON can be determined by
  42. // trial and error.  Example result: if the car is following a path with
  43. // radius of 100 ft, and if CORN_SPD_CON is 5.0, then corn_speed is 50 ft/sec.
  44. double corner_spd(double radius, double width)
  45. {
  46.    if(radius < 0.0)         // change sign of negative radius
  47.       radius = -radius;
  48.    else if(radius == 0.0)   // This is just insurance, this funtion doesn't
  49.       return(250.0);        // make sense when the radius is zero.
  50.    return CORN_SPD_CON * sqrt(radius + .5 * width);
  51. }
  52.  
  53. // In order to set vc, if you know how fast you want to go (goal), and how
  54. // fast you are going now (present), This function will compute a reasonable
  55. // value for vc.  The value is never very far from the present speed, both
  56. // to attempt to stay within the power limit, and to maintain steering control.
  57. // You can adjust the resulting slip by changing SLIP_LIM.
  58. double wheel_speed(double goal, double present)
  59. {
  60.    double ws;
  61.  
  62.    if(present > goal + 2 * SLIP_LIM)  // if too fast,
  63.       ws = present - SLIP_LIM;      // slow down.
  64.    else if(present < goal - 2 * SLIP_LIM)  // if too slow,
  65.       ws = present + SLIP_LIM;             // accelerate.
  66.    else                           // if quite close,
  67.       ws = (goal + present) / 2;      // approach desired speed gently.
  68.  
  69.    return ws;
  70. }
  71.  
  72. /* These two structures from CAR.H are repeated here as comments, because
  73.    the "driver" function receives situation as input and produces con_vec
  74.    as output.
  75. struct situation {       // a car's local situation as seen by the driver
  76.    double cur_rad;       // radius of inner wall of curve (0 means straight)
  77.    double cur_len;       // length of current track segment (angle if curve)
  78.    double to_lft;        // distance to left wall
  79.    double to_rgt;        // distance to right wall
  80.    double to_end;        // how far to end of current track seg. (angle or feet)
  81.    double v;             // the speed of the car, feet per second
  82.    double vn;            // component of v perpendicular to track direction
  83.    double nex_len;       // length of the next track segment (angle if curve)
  84.    double nex_rad;       // radius of inner wall of next segment (or 0)
  85.    double after_rad;     // radius of the segment after that one. (or 0)
  86.    double power_req;     // ratio: power requested by driver to maximum power
  87.    int dead_ahead;       // set when there is a car dead ahead, else 0
  88.    // SEE CAR.H FOR THE COMPLETE STRUCTURE - (this is only half of it)
  89. };
  90. struct con_vec { double alpha, vc; };  // control vector, steering & throttle
  91. */
  92.  
  93. // The task of this function is to compute vc and alpha.  A high speed
  94. // car on a track is a little like the keel of a boat; if you set the keel
  95. // at a slight angle to the direction of the oncoming water, you get a large
  96. // force to the side.  That is how we corner the car.  The driver sets the
  97. // car at a slight angle with respect to its direction of motion, this
  98. // cause a force to the side, causing the path of the car to curve.  The
  99. // magnitude of the force is proportional to the angle (alpha) for very
  100. // small alpha, and when there is not much wheel spin.  The wheel spin
  101. // is controlled by vc, which is the rearward speed of the bottom of the
  102. // tire.  When going down the straight at a constant, moderate velocity,
  103. // then vc is equal to the speed of the car.  For acceleration, vc is
  104. // made a little greater than the speed.  For braking, it is made a little
  105. // less.  When accelerating, vc is limited by the power available.
  106. con_vec cntrl0(situation &s)
  107. {
  108.    const char name[] = "Annie";      // This is the robot driver's name!
  109.    static int init_flag = 1;          // cleared by first call
  110.    double speed;                      // target speed for cornering, ft/sec
  111.    double speed_next;                 // target speed for next corner
  112.    con_vec result;                    // This is what is returned.
  113.    double width;                      // track width, feet
  114.    double alpha, vc;           // components of result
  115.    static double alpha_inc = 0.0;  // alpha increment during passing maneuver
  116.    static int counting = 0;    // will be set and counting down when passing
  117.    // This paragraph has nothing to do with car control; it is just
  118.    // to identify the driver by copying its name to a global RAM area:
  119.    // This happens only on the very first call to this function
  120.  
  121.    if(init_flag)  {            // first time through, only copy name:
  122.       my_name_is(name);        // copy the name string into the host program
  123.       init_flag = 0;
  124.       result.alpha = result.vc = 0;
  125.       return result;
  126.    }
  127.  
  128.   if(stuck(s.backward, s.v,s.vn, s.to_lft,s.to_rgt, &result.alpha,&result.vc))
  129.       return result;
  130.  
  131.    // Set alpha based on a servo-mechanism approach, trying to stay
  132.    // in the middle of the track, i.e., s.to_left equal to .5 * width:
  133.    width = s.to_lft + s.to_rgt;                        // find width of track
  134.    alpha = STEER_GAIN * (s.to_lft - .5 * width) / width;
  135.    alpha -= STEER_DAMP * s.vn / s.v;  // This is damping, to prevent oscillation
  136.  
  137.    // calculate target speeds for current corner and the next:
  138.    speed = corner_spd(s.cur_rad, width);      // speed is based on radius
  139.    speed_next = corner_spd(s.nex_rad, width); // of center line of track.
  140.  
  141.    // now set the tire speed, vc:
  142.    if(s.cur_rad == 0.0)                  // If we are on a straightaway,
  143.       if(s.to_end > END_ACCEL * s.cur_len) // if we are far from the end,
  144.          vc = s.v + SLIP_CON / s.v;      // keep accellerating near full power
  145.       else                    // otherwise,
  146.          vc = wheel_speed(speed_next, s.v);    // brake for next corner
  147.    else                              // If we're in the curve, maintain speed.
  148.       if(s.to_end * (fabs(s.cur_rad) + .5 * width) > END_CORNER * width)
  149.                   // if we are far from the next corner, stay at "speed".
  150.          vc = wheel_speed(speed, s.v);
  151.       else        // but when we near the next corner, adjust to "speed_next"
  152.          vc = wheel_speed(speed_next, s.v);
  153.  
  154.    // The passing maneuver:
  155.    if(s.dead_ahead & !counting)  {  // When first encountering the car ahead:
  156.       counting = PASSING_TIME;         // setup the timer,
  157.       if(rand() < RAND_MAX/2)        // choose a right or left maneuver:
  158.          alpha_inc = SHARP_TURN;
  159.       else
  160.          alpha_inc = -SHARP_TURN;
  161.    }
  162.    if(counting)  {                // If we are still in the passing maneuver,
  163.       alpha += alpha_inc;              // change alpha
  164.       --counting;                      // count down to zero
  165.    }
  166.  
  167.    result.vc = vc;   result.alpha = alpha;
  168.    return result;
  169. }
  170.